package com.video.videos.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.video.common.result.PageResult;
import com.video.videos.domain.dto.InsertCommentDto;
import com.video.videos.domain.dto.QueryCommentDto;
import com.video.videos.domain.dto.QueryPageDto;
import com.video.videos.domain.entity.Comment;
import com.video.videos.mapper.CommentMapper;
import com.video.videos.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author la
 * @since 2024-11-13
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    /*
     * 分页查询评论列表
     * */
    @Override
    public PageResult queryCommentList(QueryCommentDto dto) {
        // 参数校验
        dto.validateParams();

        // 分页获取数据
        Page<Comment> page = lambdaQuery()
                .eq(Comment::getVideoId, dto.getVideoId())
                .orderByDesc(Comment::getCreateTime)
                .page(new Page<>(dto.getPage(), dto.getPageSize()));

        return new PageResult(page.getTotal(), page.getRecords());
    }


    /*
     * 实现发布评论方法
     * */
    @Override
    public void publishComment(InsertCommentDto dto) {
        // 复制参数
        Comment comment = BeanUtil.copyProperties(dto, Comment.class);
        // 补齐参数
        comment.setReplySize(0);
        comment.setUpvote(0);
        comment.setCreateTime(System.currentTimeMillis());

        save(comment);
    }
}
